﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.HomeLoan;
using CE.Global;

public partial class home_loan_mortgage_calculator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            divResult.Visible = false;
            int _startYear = 5;
            int _endYear = 35;
            //for (int i = _startYear; i <= _endYear; i += 5)
            //{
            //    ddlLoanTenure.Items.Add(new ListItem(i.ToString(), i.ToString()));
            //}
            //ddlLoanTenure.Items.Insert(0, new ListItem("Select", "-1"));


            //ddlLoanTenure.Attributes.Add("onblur", "ShowValidationEffect('" + ddlLoanTenure.ClientID + "','" + rfvLoanTenure.ClientID + "');");
            txtLoanAmount.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtLoanAmount.ClientID + "','" + rfvLoanAmount.ClientID + "','" + revLoanAmount.ClientID + "');");
            txtIntrestRate.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtIntrestRate.ClientID + "','" + rfvIntrestRate.ClientID + "','" + revIntrestRate.ClientID + "');");
            txtLoanTenure.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtLoanTenure.ClientID + "','" + rfvLoanTenure.ClientID + "','" + revLoanTenure.ClientID + "');");
            
            General.ClearAllSession(this.Page, "HL");
            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();
        }

    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        divResult.Visible = false;
        ChangeValidationEffectServerSide("homeloan");
        if (Page.IsValid)
        {
            float principal = Convert.ToSingle(txtLoanAmount.Text);
            float interest = Convert.ToSingle(txtIntrestRate.Text);
            //Int32 noy = Convert.ToInt32(ddlLoanTenure.Text);
            Int32 noy = Convert.ToInt32(txtLoanTenure.Text);

            
            if (principal == 0) 
            {
                lblError.Text = "Please fill valued Loan Amount.";
            }
            else if (interest == 0) 
            {
                lblError.Text = "Please fill valued Interest Rate.";
            }
            else if (noy == 0)
            {
                lblError.Text = "Please fill valued Length of Loan.";
            }
            else
            {
                lblError.Text = "";
            }

            if (principal != 0f && interest != 0f && noy != 0) {
                //var str = principal
                //var a = (str.replace(/,/, ""))
                float t_years =  noy * 12;
                float t_interest = interest / 1200f;
                double t = (1.0 / (Math.Pow((1 + t_interest), t_years)));
                double payment = 0;
                if (t < 1) 
                {
                    payment = ((principal * t_interest) / (1 - t));
                }
                divResult.Visible = true;
                string total = String.Format("${0:f}", payment);
                string loanamount = "0";
                if (principal >= 1000)
                    loanamount = principal.ToString("$#,000");
                else
                    loanamount = principal.ToString("$#");

                lblMonthlyPayment.Text = total;
                lblLoanAmount.Text = loanamount;
                lblIntrestRate.Text = interest.ToString() + "%";
                lblLoanDuration.Text = noy.ToString() + " years";
                //lblResult.Text = "Your Monthly Payment for " + noy + " Years at an Interest Rate of " + interest + "% for a Loan Amount of " + loanamount + " is " + total + " a Month";
                
            }
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Loan Amount
        if (rfvLoanAmount.Enabled && rfvLoanAmount.ValidationGroup == strValidationGroup)
        {
            rfvLoanAmount.Validate();
            if (rfvLoanAmount.IsValid == false)
            {
                txtLoanAmount.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtLoanAmount.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtLoanAmount.ControlStyle.CssClass = "";
        }

        if (revLoanAmount.Enabled && revLoanAmount.ValidationGroup == strValidationGroup)
        {
            if (rfvLoanAmount.IsValid)
            {
                revLoanAmount.Validate();
                if (revLoanAmount.IsValid == false)
                {
                    txtLoanAmount.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtLoanAmount.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtLoanAmount.ControlStyle.CssClass = "";
        }

        //IntrestRate
        if (rfvIntrestRate.Enabled && rfvIntrestRate.ValidationGroup == strValidationGroup)
        {
            rfvIntrestRate.Validate();
            if (rfvIntrestRate.IsValid == false)
            {
                txtIntrestRate.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtIntrestRate.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtIntrestRate.ControlStyle.CssClass = "";
        }

        if (revIntrestRate.Enabled && revIntrestRate.ValidationGroup == strValidationGroup)
        {
            if (rfvIntrestRate.IsValid)
            {
                revIntrestRate.Validate();
                if (revIntrestRate.IsValid == false)
                {
                    txtIntrestRate.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtIntrestRate.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtIntrestRate.ControlStyle.CssClass = "";
        }

        //LoanTenure
        //if (rfvLoanTenure.Enabled && rfvLoanTenure.ValidationGroup == strValidationGroup)
        //{
        //    rfvLoanTenure.Validate();
        //    if (rfvLoanTenure.IsValid == false)
        //    {
        //        ddlLoanTenure.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        ddlLoanTenure.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlLoanTenure.ControlStyle.CssClass = "";
        //}

        //LoanTenure
        if (rfvLoanTenure.Enabled && rfvLoanTenure.ValidationGroup == strValidationGroup)
        {
            rfvLoanTenure.Validate();
            if (rfvLoanTenure.IsValid == false)
            {
                txtLoanTenure.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtLoanTenure.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtLoanTenure.ControlStyle.CssClass = "";
        }

        if (revLoanTenure.Enabled && revIntrestRate.ValidationGroup == strValidationGroup)
        {
            if (rfvLoanTenure.IsValid)
            {
                revIntrestRate.Validate();
                if (revIntrestRate.IsValid == false)
                {
                    txtLoanTenure.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtLoanTenure.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtLoanTenure.ControlStyle.CssClass = "";
        }


    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogViewedHistory("HL", Request, "Home loan mortgage calculator.");
    }
}
